/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Beans;

import BLL.StaffBLL;
import DatabaseAccess.SQLHelper;
import Entities.TblClass;
import Entities.TblStaff;
import java.sql.CallableStatement;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author CuongNV
 */
@ManagedBean
@SessionScoped
public class UserManagedBean extends TblStaff {

    TblStaff staff = null;
    String checkLog;
    String username;
    String password;

    /** Creates a new instance of UserManagedBean */
    public UserManagedBean() {
    }

    public List<TblClass> getAllByBranchId() {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
        TblStaff staff = (TblStaff) session.getAttribute("staff");
        return new BLL.ClassBLL().getAllByBranchId(staff.getBranchId());
    }
    public String login() {
        int check = checkLogin();

        if (check == 1) {
            StaffBLL staffBLL = new StaffBLL();
            staff = staffBLL.getStaffByUsername(username);
            setSession("staff", staff);
            String log=null;
            if (staff.getRoleId() == 1) { //Admin
                log= "admin/academiclist.xhtml?faces-redirect=true";
            } else if (staff.getRoleId() == 2) {//Academic department
                log= "manager/default.xhtml?faces-redirect=true";
            } else if (staff.getRoleId() == 3) {//faculty
                log= "faculty/index.xhtml?faces-redirect=true";
            }
            return log;
        } else {
            return "login";
        }
    }

    public String check() {
        if (getSession("staff") == null) {
            checkLog = "Login";
        } else {
            checkLog = "Logout";
        }

        return checkLog;
    }

    public String actionCheck(String log) {
        if (log.equalsIgnoreCase("Logout")) {
            HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
            session.removeAttribute("staff");
            checkLog = "Login";
        }

        return "login";
    }

    public static void setSession(String key, Object value) {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        session.setAttribute(key, value);
    }

    public static Object getSession(String key) {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        return session.getAttribute(key);
    }

    public String logout() {
        try {
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().clear();
            return "/index.xhtml?faces-redirect=true";
        } catch (Exception ex) {
            return "academiclist";
        }
    }

    public int checkLogin() {
        CallableStatement objCall;
        int result = -1;
        String[] params = new String[3];
        params[0] = username;
        params[1] = password;
        params[2] = null;

        try {
            objCall = SQLHelper.execute("procLogin", params);
            result = objCall.getInt("Result");
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
        }

        return result;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @Override
    public void setUsername(String username) {
        this.username = username;
    }

    public String getCheckLog() {
        return checkLog;
    }

    public void setCheckLog(String checkLog) {
        this.checkLog = checkLog;
    }
}
